package cn.data.stream.config;

import cn.data.stream.annotation.EnableDataStreamConfig;
import cn.data.stream.config.parse.DataStreamBeanNameGenerator;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.type.AnnotationMetadata;

/**
 * @author shi hong yu
 */
public class DataStreamConfigRegistrar implements ImportBeanDefinitionRegistrar, ResourceLoaderAware {
    private ResourceLoader resourceLoader;
    private DataStreamBeanNameGenerator beanNameGenerator =new DataStreamBeanNameGenerator();

    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        AnnotationAttributes attributes = AnnotationAttributes
            .fromMap(importingClassMetadata.getAnnotationAttributes(EnableDataStreamConfig.class.getName()));

        String[] namespaces = attributes.getStringArray("values");
        int order = attributes.getNumber("order");
       DataStreamClassPathBeanDefinitionScanner scanner = new DataStreamClassPathBeanDefinitionScanner(registry,false);
        scanner.setResourceLoader(resourceLoader);
        /**
         * 此处过滤的为 : 仅注册增加了 @MyComponent 注解的类，如想实现扫描包下的所有类，则无需注册过滤器
         */
        scanner.setBeanNameGenerator(beanNameGenerator);
        scanner.registerFilters();
        scanner.doScan(namespaces);
    }

    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }
}
